﻿using DotNetOpenAuth.AspNet.Clients;
using DotNetOpenAuth.OAuth.ChannelElements;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BoriPeter.NutritionManager.FatSecret
{
    public class FatSecretTokenManager : IConsumerTokenManager, ITokenManager
    {   
        #region Public constructor
        public FatSecretTokenManager(string consumerKey, string consumerSecret, IOAuthTokenManager tokenManager)
        {
            _consumerKey = consumerKey;
            _consumerSecret = consumerSecret;
        }
        #endregion

        private string _consumerKey;
        public string ConsumerKey
        {
            get { return _consumerKey; }
        }

        private string _consumerSecret;
        public string ConsumerSecret
        {
            get { return _consumerSecret;  }
        }

        private static string _token;
        private static string _tokenSecret;
        private static TokenType _tokeType = TokenType.InvalidToken;

        public void ExpireRequestTokenAndStoreNewAccessToken(string consumerKey, string requestToken, string accessToken, string accessTokenSecret)
        {
            _token = accessToken;
            _tokenSecret = accessTokenSecret;
            _tokeType = TokenType.AccessToken;

            MessageBase.OAuthToken = _token;
            MessageBase.OAuthTokenSecret = _tokenSecret;
        }

        public string GetTokenSecret(string token)
        {
            return _tokenSecret;
        }

        public TokenType GetTokenType(string token)
        {
            return _tokeType;
        }

        public void StoreNewRequestToken(DotNetOpenAuth.OAuth.Messages.UnauthorizedTokenRequest request, DotNetOpenAuth.OAuth.Messages.ITokenSecretContainingMessage response)
        {
            _token = response.Token;
            _tokenSecret = response.TokenSecret;
            _tokeType = TokenType.RequestToken;
        }
    }
}
